package com.hqbzl.chat.handler;

import static java.util.stream.Collectors.joining;

import java.util.Set;

import com.hqbzl.chat.factory.Factory;
import com.hqbzl.chat.model.GMemgersMessage;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@Sharable
public final class GroupMemberMessageHandler extends SimpleChannelInboundHandler<GMemgersMessage> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, GMemgersMessage msg) throws Exception {
		String groupName = msg.getGroupName();
		Set<String> members = Factory.groupSession.getMembers(groupName);
		String collect = members.stream().collect(joining(","));
		GMemgersMessage build = GMemgersMessage.builder().groupName(groupName).members(collect).build();
		ctx.writeAndFlush(build);
	}
}